import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {MessageService, ModalComponent, PaginationService, ProgressBarService, Table} from '../../../../../ng-tools';
import {TicketService} from '../../../../services/api/ticket.service';
import {ActivatedRoute, Router} from '@angular/router';
import {HttpParams} from '@angular/common/http';
import {SystemGroupFormatterService} from '../../../../services/formatter/system-group-formatter.service';
import {PesonalGroupMoneyService} from '../../../../services/formatter/pesonal-group-money.service';
import {PersonalGroupEditComponent} from '../personal-group-edit/personal-group-edit.component';

@Component({
  selector: 'personal-group-detail',
  templateUrl: './personal-group-detail.component.html',
  styleUrls: ['./personal-group-detail.component.scss']
})
export class PersonalGroupDetailComponent implements OnInit, AfterViewInit {

  @ViewChild('modal') private modal: ModalComponent;
  breadcrumb = [
    {name: '私人群', cssIcon: 'fa fa-fw fa-podcast', link: '/main/oplatform/personal-group'},
    {name: '详情', cssIcon: 'fa fa-fw fa-bars'}
  ];
  page = 1;
  editRobotData;
  deleteRobotData;
  currentGroup;
  pagenation = new PaginationService(null, null);
  personalMoneyPagenation = new PaginationService(null, null);
  personalMoneyTable = new Table({
    headers: this.personalMoneyFormatter.formatter
  });
  chargeTable = new Table({
    headers: this.moneyFormatter.personalDetailFormatter,
    buttons: [
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-pencil-square',
        text: '修改',
        isShow: true,
        click: (e) => {
          this.editRobotData = e;
          this.showEditRobot();
        }
      }
    ]
  });

  constructor(private message: MessageService,
              private ticketService: TicketService,
              public progress: ProgressBarService,
              private routeInfo: ActivatedRoute,
              private router: Router,
              private personalMoneyFormatter: PesonalGroupMoneyService,
              private moneyFormatter: SystemGroupFormatterService,
  ) {
    try {
      this.currentGroup = JSON.parse(this.routeInfo.queryParams['_value']['gameGroup']);
    } catch (e) {
      this.currentGroup = null;
    }
  }

  ngOnInit() {
    this.getInfo();

  }
  showEditRobot() {
    this.modal.show({
      title: '修改群主返利比',
      cssIcon: 'fa fa-fw fa-pencil-square',
      dynamicComponent: {
        component: PersonalGroupEditComponent,
        data: this.editRobotData,
        events: {
          success: (data) => {
            this.modal.hide();
            this.currentGroup.master_fl_perc = data.masterFlPerc;
            this.getInfo();
          }
        },
      },
      data: null
    });
  }
  modalEvenHandler() {
    this.modal.event.subscribe({
      next: (event) => {
        if (event.name === 'confirm' && event.id === 'deleteRobot') {
          this.deleteRobot();
        }
      }
    });
  }
  getInfo() {
    this.pagenation.simpleFormatter = this.chargeTable.options.headers;
    this.pagenation.page.results = [this.currentGroup];
    this.pagenation.formatData();
    this.personalMoneyPagenation.simpleFormatter = this.personalMoneyTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getGroupMoneyInfo, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('gId', this.currentGroup.gid)}).subscribe((resp) => {
      if (resp.body.success) {
        const result = resp.body.returnValue;
        result.todayInfo.dateTime = '今日数据';
        result.threeTodayInfo.dateTime = '近三天数据';
        result.sevenTodayInfo.dateTime = '近七天数据';
        result.monthInfo.dateTime = '本月数据';
        this.personalMoneyPagenation.page.results = [result.todayInfo, result.threeTodayInfo, result.sevenTodayInfo, result.monthInfo];
        this.personalMoneyPagenation.formatData();
        this.message.sendMessage('page load progress', this);
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  deleteRobot() {
    this.ticketService.post(this.ticketService.urls.delRobotFromRobotGroup, {userId: this.deleteRobotData.userUid}, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
      if (resp.body.success) {
        this.modal.hide();
        this.getInfo();
      }
    });
  }
  ngAfterViewInit(): void {
    this.modalEvenHandler();
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    this.message.sendMessage('page load progress', this, '300ms');
  }

}
